
package top.chengdongqing.common.kit;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

import com.alibaba.druid.util.StringUtils;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.StrKit;

import top.chengdongqing.common.model.User;

/**
 * 字符串处理工具类
 * 
 * @author Luyao
 *
 */
public class StringKit {
	
	
	/**
	 * 生成数据库id
	 * @return
	 */
	public static String generateDataId() {
		return UUID.randomUUID().toString();
	}
	
	
	/**
	 * 功能:生成加密后的密码
	 * @param salt
	 * @param password
	 * @return
	 */
	public static String generateEncryptPassword(String salt,String password){
		StringBuilder stb=new StringBuilder();
		if(StringUtils.isEmpty(salt) || StringUtils.isEmpty(password)) {
			System.out.println("请检查相关参数是否未传,生成失败!");
			return null;
		}
	    stb.append(HashKit.sha256(password+salt));		
		return stb.toString();
	}
	
	
	/**
	 * 功能:根据登录账号生成当前用户盐值
	 * @param account 账号
	 * @return
	 */
	public static String getRandomSalt() {
			String model = "abcdefghijklmnopqrstuvwxyz1234567890";
			StringBuffer salt = new StringBuffer();
			char[] m = model.toCharArray();
			for (int i = 0; i < 6; i++) {
				char c = m[(int) (Math.random() * 36)];
				salt = salt.append(c);
		    }
			String result=salt.toString();
		    return result;
     }


	/**
	 * 生成验证码
	 * 
	 * @return
	 */
	public static String generateRandomCode() {
		String randNum = String.valueOf(Math.random());
		return randNum.substring(randNum.length() - 6);
	}

	/**
	 * 生成订单号
	 * 
	 * @return
	 */
	public static String generateOrderNo() {
		return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMddHHmmss")) + generateRandomCode();
	}

	/**
	 * 过滤账号
	 * 
	 * @param account
	 * @return
	 */
	public static String filterAccount(String account) {
		if (StrKit.notBlank(account)) {
			if (account.matches(User.Pattern.PHONE)) {
				// 手机号取前3位和后2位
				String prefix = account.substring(0, 3);
				String suffix = account.substring(9);
				return prefix + "******" + suffix;
			} else if (account.matches(User.Pattern.EMAIL)) {
				// 邮箱取前2位和@符号前一位到最后一位
				String prefix = account.substring(0, 2);
				int index = account.indexOf("@") - 1;
				String suffix = account.substring(index);
				return prefix + "******" + suffix;
			}
		}
		return "";
	}
	
	public static void main(String[] args) {
	//  String randomSalt=getRandomSalt();
	  //System.out.println(randomSalt);
	   String generatePassword=generateEncryptPassword("vxpu7r", "123456789");
	   System.out.println(generatePassword);
	}
}
